﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.Logging;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using VisionApp.UserManager.Service;

namespace VisionApp.Startup.ViewModels
{
    public partial class SideBarMenuViewModel:ObservableObject
    {
        [ObservableProperty]
        private bool hasPermission;
        private readonly IUserManagerService userManagerService;
        private readonly ILogger<SideBarMenuViewModel> logger;
        private UserDto? currentUser;
        public SideBarMenuViewModel(IUserManagerService userManagerService, ILogger<SideBarMenuViewModel> logger)
        {
            this.userManagerService = userManagerService;
            this.logger = logger;

            currentUser = new UserDto() { UserName = "User", Level = UserLevel.user };
            userManagerService.OnUserChanged += UserManagerService_OnUserChanged; ;
        }
        private void UserManagerService_OnUserChanged(object sender, UserDto e)
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                currentUser = e;
                HasPermission = currentUser.Level>UserLevel.user;
            });
        }

        [RelayCommand]
        public void Login()
        {

        }

        private void LoginView_LoginSuccess(object sender, bool e)
        {
   
        }

        [RelayCommand]
        public void OpenSettings()
        {

        }
        // 显示日志文件
        [RelayCommand]
        public void OpenLogFolder()
        {
            var folder=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"logs");
            try
            {
                Process.Start( folder);
            }
            catch (Exception ex)
            {
                logger.LogError($"打不开文件夹{folder}");
            }
        }
        [RelayCommand]
        public void Test()
        {

        }
        [RelayCommand]
        private void OpenCalc()
        {
            Process.Start("calc.exe");
        }

        [DllImport("user32.dll")]
        private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

        private const int KEYEVENTF_KEYDOWN = 0x0000;
        private const int KEYEVENTF_KEYUP = 0x0002;
        private const byte VK_LWIN = 0x5B;
        private const byte VK_SHIFT = 0x10;
        private const byte VK_S = 0x53;
        [RelayCommand]
        private void OpenSnippingTool()
        {
            try
            {
                // 模拟按下 Win + Shift + S 快捷键
                keybd_event(VK_LWIN, 0, KEYEVENTF_KEYDOWN, 0);
                keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYDOWN, 0);
                keybd_event(VK_S, 0, KEYEVENTF_KEYDOWN, 0);

                // 释放按键
                keybd_event(VK_S, 0, KEYEVENTF_KEYUP, 0);
                keybd_event(VK_SHIFT, 0, KEYEVENTF_KEYUP, 0);
                keybd_event(VK_LWIN, 0, KEYEVENTF_KEYUP, 0);

                logger.LogInformation("已启动截图工具，请选择截图区域。");
            }
            catch (Exception ex)
            {
                // 备用方案：尝试直接调用 SnippingTool.exe
                try
                {
                    Process.Start("SnippingTool.exe");
                    Console.WriteLine("已启动传统截图工具。");
                }
                catch (Exception innerEx)
                {
                    logger.LogError($"备用截图工具也无法启动: {innerEx.Message}");
                }
            }
        }
    }
}
